package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeam;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityTeamRankMgr;
import com.yanqu.road.server.manager.douluodalu.CrossDouLuoDaLuContext;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = Protocol.C2_CROSS_GET_ACTIVITY_TEAM_RANK_REWARD, desc = "领取跨服活动队伍奖励")
public class CrossActivityTeamRankRewardCmd extends CrossUnionCommand {

    private static Logger logger = LogManager.getLogger(CrossActivityTeamRankRewardCmd.class);

    private int activityId;
    private int conditionType;
    private int conditionId;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossGeneralActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossGeneralActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        conditionId = reqMsg.getConditionId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            return;
        }
        int teamId = 0;
        long teamServerId = 0;
        if(activityInfo.getType() == eActivityType.DouLuoDaLuActivity.getValue()){
            CrossDouLuoDaLuContext context = CrossDouLuoDaLuContext.initContext(activityId, userId).loadTeamData();
            if(context.getRet() != 0){
                return;
            }
            teamId = context.getTeamData().getTeamId();
            teamServerId = context.getTeamData().getCaptainServerId();
        }

        if(teamId > 0){
            int rank = ActivityTeamRankMgr.getCrossActivityTeamRank(activityId, conditionType, teamServerId, teamId, groupId);
            CrossActivityProto.CrossGeneralActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossGeneralActivityGetRewardRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setConditionType(conditionType);
            respMsg.setConditionId(conditionId);
            respMsg.setRank(rank);
            respMsg.setPosition(0);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_ACTIVITY_GET_TEAM_RANK_REWARD_NOTIFY, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }

    }

    @Override
    public void sendData() {

    }
}
